package com.edgclub.edg.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

import com.edgclub.edg.R;


/**
 * 通知栏的信息
 * 
 * @author chenkb
 * 
 */
public class NotificationClient {

	private Context mContext;
	private NotificationManager mNotificationManager;
	private int notifyId = 1;

	private static NotificationClient mNotificationClient;

	private NotificationClient(Context mContext) {
		this.mContext = mContext;
		if (null == mNotificationManager) {
			mNotificationManager = (NotificationManager) mContext
					.getSystemService(Context.NOTIFICATION_SERVICE);
		}

	}

	public static NotificationClient create(final Context mContext) {
		if (null == mNotificationClient) {
			mNotificationClient = new NotificationClient(mContext);
		}

		return mNotificationClient;
	}

	/**
	 * 显示通知
	 * 
	 * @param title
	 * @param message
	 */
	public void show(String title, String message, Intent resultIntent) {
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
				mContext).setContentTitle(title).setContentText(message)
				.setSmallIcon(R.drawable.appicon)
				.setDefaults(Notification.DEFAULT_ALL)
				.setWhen(System.currentTimeMillis())
				.setPriority(1000)
				.setAutoCancel(true);

		TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
		// Adds the Intent to the top of the stack
		stackBuilder.addNextIntent(resultIntent);
		// Gets a PendingIntent containing the entire back stack
		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
				PendingIntent.FLAG_UPDATE_CURRENT);

		mBuilder.setContentIntent(resultPendingIntent);
		mNotificationManager.notify(notifyId, mBuilder.getNotification());
		notifyId++;
	}

	/**
	 * 取消通知
	 */
	public void cancel() {
		if (null != mNotificationManager) {
			mNotificationManager.cancel(notifyId);
		}
	}
}
